package com.asm.DBloader;

import java.util.ArrayList;

import android.R.string;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class ASM_AVTempDS {
	private SQLiteDatabase dB;
	private ASM_DB dbHelper;
	Context context;
	
	
	public ASM_AVTempDS(Context context) {
		this.context = context;
		dbHelper  = new ASM_DB(context);
		
	}
	
	public void open() throws SQLException
	{
		dB = dbHelper.getWritableDatabase();
	}
	
	public int Create(ArrayList<ASM_AVTemp>  list) 
	{
		
		int serverdbID =0;
		
		if(dB == null){
			open();
		}else if(!dB.isOpen())
		{
			open();
		}
		Cursor cursor = null;
		for(ASM_AVTemp loc : list)
		{
			
		
			String selectQuery = "SELECT * FROM fAvtemp WHERE itemcode = '"+loc.getAsmavTempItemcode()+"'";
			cursor = dB.rawQuery(selectQuery, null);
		
		ContentValues values = new ContentValues();
		values.put(ASM_DB.asmavTempRefno, loc.getAsmavTempRefno());
		values.put(ASM_DB.asmavTempItemname, loc.getAsmavTempItemname());
		values.put(ASM_DB.asmavTempItemcode, loc.getAsmavTempItemcode());
		values.put(ASM_DB.asmavTempBrandcode, loc.getAsmavTempBrandcode());
		
		values.put(ASM_DB.asmavTempStatus,loc.getAsmavTempStatus());
		values.put(ASM_DB.asmavTempNewsales,loc.getAsmavTempNewsales());
		values.put(ASM_DB.asmavTempUitems,loc.getAsmavTempUitems());
		values.put(ASM_DB.asmavTempUstatus,loc.getAsmavTempUstatus());
		values.put(ASM_DB.asmavTempHitems,loc.getAsmavTempHitems());
		values.put(ASM_DB.asmavTempHstatus,loc.getAsmavTempHstatus());
		values.put(ASM_DB.asmavTempVitems,loc.getAsmavTempVitems());
		values.put(ASM_DB.asmavTempVstatus,loc.getAsmavTempVstatus());
		values.put(ASM_DB.asmavTempActivestate,loc.getAsmavTempActivestate());
	
		
		int count = cursor.getCount();
		if (count>0) {
			serverdbID = (int) dB.update(ASM_DB.ASM_AVTEMP, values, "itemcode = '"+loc.getAsmavTempItemcode()+"'", null);
		}else {
			serverdbID = (int) dB.insert(ASM_DB.ASM_AVTEMP, null, values);
		}
		
		
		}
		dB.close();
		return serverdbID;
		
	}
	public int RefNoReset(ASM_AVTemp det) 
	{
		
		int count =0;
		
		if(dB == null)
		{
			open();
		}else if(!dB.isOpen())
		{
			open();
		}

		ContentValues values = new ContentValues();
		values.put(ASM_DB.asmavTempRefno, "");
	//	values.put(ASM_DB.asmavTempItemname, det.getAsmavTempItemname());
	//	values.put(ASM_DB.asmavTempItemcode, det.getAsmavTempItemcode());
		values.put(ASM_DB.asmavTempStatus,"");
		values.put(ASM_DB.asmavTempNewsales,"");
		values.put(ASM_DB.asmavTempUitems,"");
		values.put(ASM_DB.asmavTempUstatus,"");
		values.put(ASM_DB.asmavTempHitems,"");
		values.put(ASM_DB.asmavTempHstatus,"");
		values.put(ASM_DB.asmavTempVitems,"");
		values.put(ASM_DB.asmavTempVstatus,"");
		
		String skds = det.getAsmavTempRefno().toString();
	
		count = (int) dB.update(ASM_DB.ASM_AVTEMP, values, "RefNo = '"+det.getAsmavTempRefno()+"'", null);

		dB.close();
		return count;
		
	}
	public ArrayList<ASM_AVTemp> getTempdetail(String brandcode,String refno) 
	{
		if (dB == null) {
			open();
		} else if (!dB.isOpen()) {
			open();
		}
		
	
		String details = null;
		String selectQuery = "select itemname,itemcode,brandcode,status,newsales,uitems,ustatus,hitems,hstatus,vitems,vstatus from fAvtemp where brandcode = '"+brandcode+"' ";
		
		Cursor cursor = null;
		cursor = dB.rawQuery(selectQuery, null);
		ArrayList<ASM_AVTemp> list = new ArrayList<ASM_AVTemp>();
		while(cursor.moveToNext())
		{
			ASM_AVTemp temp=new ASM_AVTemp();
			temp.setAsmavTempRefno(refno);
			temp.setAsmavTempItemname(cursor.getString(0));
			temp.setAsmavTempItemcode(cursor.getString(1));
			temp.setAsmavTempBrandcode(cursor.getString(2));
			temp.setAsmavTempStatus(cursor.getString(3));
			temp.setAsmavTempNewsales(cursor.getString(4));
			temp.setAsmavTempUitems(cursor.getString(5));
			temp.setAsmavTempUstatus(cursor.getString(6));
			temp.setAsmavTempHitems(cursor.getString(7));
			temp.setAsmavTempHstatus(cursor.getString(8));
			temp.setAsmavTempVitems(cursor.getString(9));
			temp.setAsmavTempVstatus(cursor.getString(10));

		list.add(temp);
		
		
		}
		
		return list;
	}
	
	
	public ArrayList<String> getSaveTemp(String RefNo) 
	{
		if (dB == null) {
			open();
		} else if (!dB.isOpen()) {
			open();
		}
		
		ArrayList<String> Details = new ArrayList<String>();
	
		String selectQuery = "select * from fAvtemp where RefNo = '"+RefNo+"' and( status != '' or newsales != '' or uitems != '' or newsales != '' or uitems != '' or ustatus != '' or hitems != '' or hstatus != '' or vitems != '' or vstatus != '') ";
		
		Cursor cursor = null;
		cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext())
		{
	
			Details.add(cursor.getString(1)
					+"-:-"+cursor.getString(2)
					+"-:-"+cursor.getString(3)
					+"-:-"+cursor.getString(4)
					+"-:-"+cursor.getString(5)
					+"-:-"+cursor.getString(6)
					+"-:-"+cursor.getString(7)
					+"-:-"+cursor.getString(8)
					+"-:-"+cursor.getString(9)
					+"-:-"+cursor.getString(10)
					+"-:-"+cursor.getString(11));
			
			
		}
		
		return Details;
	}

	public String CheckDetails(String RefNo) 
	{
		if (dB == null) {
			open();
		} else if (!dB.isOpen()) {
			open();
		}
		
	
		String details = null;
		String selectQuery = "select * from fAvtemp where  RefNo = '"+RefNo+"' ";
		
		Cursor cursor = null;
		cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext())
		{
		
			details = cursor.getString(0)
					+"-:-"+cursor.getString(1)
					+"-:-"+cursor.getString(2)
					+"-:-"+cursor.getString(3)
					+"-:-"+cursor.getString(4)
					+"-:-"+cursor.getString(5)
					+"-:-"+cursor.getString(6)
					+"-:-"+cursor.getString(7);
		
		
		}
		
		return details;
	}
	
	public ArrayList<ASM_AVTemp> GetTempDetails(String refno) 
	{
		if (dB == null) {
			open();
		} else if (!dB.isOpen()) {
			open();
		}
		
	
		String details = null;
		String selectQuery = "select "
				+ "itemname,"
				+ "itemcode,"
				+ "brandcode,"
				+ "status,"
				+ "newsales,"
				+ "uitems,"
				+ "ustatus,"
				+ "hitems,"
				+ "hstatus,"
				+ "vitems,"
				+ "vstatus from fAvtemp where RefNo = '"+refno+"' and( status != '' or newsales != '' or uitems != '' or newsales != '' or uitems != '' or ustatus != '' or hitems != '' or hstatus != '' or vitems != '' or vstatus != '') ";
		//and newsales != '' and uitems != '' and ustatus != '' and hitems != '' and hstatus != '' and vitems != '' and vstatus != ''
		
		Cursor cursor = null;
		cursor = dB.rawQuery(selectQuery, null);
		
		ArrayList<ASM_AVTemp> list = new ArrayList<ASM_AVTemp>();
		while(cursor.moveToNext())
		{
			ASM_AVTemp temp=new ASM_AVTemp();
			temp.setAsmavTempRefno(refno);
			temp.setAsmavTempItemname(cursor.getString(cursor.getColumnIndex(ASM_DB.asmavTempItemname)));
			temp.setAsmavTempItemcode(cursor.getString(cursor.getColumnIndex(ASM_DB.asmavTempItemcode)));
			temp.setAsmavTempBrandcode(cursor.getString(cursor.getColumnIndex(ASM_DB.asmavTempBrandcode)));
			temp.setAsmavTempStatus(cursor.getString(cursor.getColumnIndex(ASM_DB.asmavTempStatus)));
			temp.setAsmavTempNewsales(cursor.getString(cursor.getColumnIndex(ASM_DB.asmavTempNewsales)));
			temp.setAsmavTempUitems(cursor.getString(cursor.getColumnIndex(ASM_DB.asmavTempUitems)));
			temp.setAsmavTempUstatus(cursor.getString(cursor.getColumnIndex(ASM_DB.asmavTempUstatus)));
			temp.setAsmavTempHitems(cursor.getString(cursor.getColumnIndex(ASM_DB.asmavTempHitems)));
			temp.setAsmavTempHstatus(cursor.getString(cursor.getColumnIndex(ASM_DB.asmavTempHstatus)));
			temp.setAsmavTempVitems(cursor.getString(cursor.getColumnIndex(ASM_DB.asmavTempVitems)));
			temp.setAsmavTempVstatus(cursor.getString(cursor.getColumnIndex(ASM_DB.asmavTempVstatus)));

		list.add(temp);
		
		
		}
		
		return list;
	}


}
